package com.wz.app.frgment;

import com.wz.app.R;
import com.wz.app.activity.NewsItemActivity;
import com.wz.app.http.GetJson;
import com.wz.app.http.LoadImage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class NewsFragment extends Fragment implements OnItemClickListener {

	private ViewFlipper flipper;
	private View view;
	private String url = "http://apis.baidu.com/showapi_open_bus/channel_news/search_news";
	// private TextView test;
	private ListView listView;
	private LoadImage loadImage;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.news, null);
		listView = (ListView) view.findViewById(R.id.news_ListView);

		initView();
		listView.setOnItemClickListener(this);
		System.out.println("onCreateView");
		
		return view;
	}

	
	private void initView() {

		flipper = (ViewFlipper) view.findViewById(R.id.news_top_viewFlipper);
		ImageView  imageView = new ImageView(getActivity());
		imageView.setImageResource(R.drawable.aa);
		ImageView  imageView2 = new ImageView(getActivity());
		imageView2.setImageResource(R.drawable.bb);
		ImageView  imageView3 = new ImageView(getActivity());
		imageView3.setImageResource(R.drawable.cc);
		flipper.setInAnimation(getActivity(), android.R.anim.fade_in);
		flipper.setOutAnimation(getActivity(), android.R.anim.fade_out);
		flipper.setFlipInterval(3000);
		
		flipper.addView(imageView);
		flipper.addView(imageView2);
		flipper.addView(imageView3);
		flipper.startFlipping();

		// 将json转成
		GetJson getJson = new GetJson(url, null, getActivity(), listView);
		
		getJson.get();
		// }
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

		TextView tv = (TextView) view.findViewById(R.id.news_listView_title);
		TextView link = (TextView) view.findViewById(R.id.news_listView_link);
		Intent intent = new Intent(getActivity(), NewsItemActivity.class);
		intent.putExtra("url", link.getText());
		intent.putExtra("title", tv.getText());
		startActivity(intent);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("onDestory");
		super.onDestroy();
	}

}
